/**
 * 
 */
package com.flashdeck.activity;

import org.apache.commons.lang.StringUtils;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import com.flashdeck.Extras;
import com.flashdeck.R;
import com.flashdeck.dao.DeckDao;
import com.flashdeck.dao.impl.DeckDaoImpl;
import com.flashdeck.domain.DeckInfo;
import com.flashdeck.domain.DeckProgress;

/**
 * @author Vincentas
 *
 */
public class DeckDetailsActivity extends Activity {
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.deck_details_layout);
		
		String deckId = getIntent().getExtras().getString(Extras.SELECTED_DECK);
		// TODO check if deckId is not null
		DeckInfo deckInfo = getDeckDao().getDeckInfo(deckId);
		// TODO check if deckInfo is not null
	
		TextView deckNameTextView = (TextView) findViewById(R.id.deckNameTextView);
		deckNameTextView.setText(deckInfo.getName());
		
		TextView cardsCountTexgtView = (TextView) findViewById(R.id.cardsCountTextView);
		cardsCountTexgtView.setText(Integer.toString(deckInfo.getCardCount()));
		
		EditText deckDescriptionEditText = (EditText) findViewById(R.id.deckDescriptionEditText);
		deckDescriptionEditText.setText(deckInfo.getDescription());
				
		updateProgress();
	}
	
	private void updateProgress() {
		String deckId = getIntent().getExtras().getString(Extras.SELECTED_DECK);
		DeckProgress deckProgress = getDeckDao().getDeckProgress(deckId);		
		DeckInfo deckInfo = getDeckDao().getDeckInfo(deckId);
		
		LinearLayout progressLayout = (LinearLayout) findViewById(R.id.progressLayout);
		progressLayout.removeAllViews();
		
		for (int i = 0, n = deckProgress.getGroups(); i < n; i++) {
			LinearLayout layout = new LinearLayout(this);
			TextView textView = new TextView(this);
			String decksInGroup = StringUtils.leftPad(Integer.toString(deckProgress.getCardsInGroup(i)), 3) + " ";
			textView.setText(decksInGroup);
			ProgressBar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
			
			progressBar.setMax(deckInfo.getCardCount());
			progressBar.setProgress(deckProgress.getCardsInGroup(i));					
			progressBar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
			layout.addView(textView);
			layout.addView(progressBar);
			progressLayout.addView(layout);
		}
	}

	/**
	 * @return the deckDao
	 */
	public DeckDao getDeckDao() {
		return DeckDaoImpl.getInstance();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		
		updateProgress();		
	}
	
}
